/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package API;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author mrbao
 */
public class PopupMenu extends Item {

    private String[] menus;
    private int margin = 2;
    private String[] menustmp;
    private int index;
    private int heightPopup;
    private int length = 5;
    private int start;
    private int end;

    public PopupMenu(String[] menus, int y) {
        this.menus = menus;
        index = 0;
        if (menus.length <= length) {
            menustmp = menus;
        } else {
            menustmp = new String[length];
            System.arraycopy(menus, 0, menustmp, 0, length);
        }
        width = 0;
        for (int i = 0; i < menustmp.length; i++) {
            String string = menus[i];
            menustmp[i] = string;
            if (font.stringWidth(string) > width) {
                width = font.stringWidth(string) + 2 * margin;
            }
        }
        start = 0;
        end = menustmp.length;
        heightPopup = font.getHeight() * menustmp.length + 2 * margin;
        this.y = y - heightPopup;
        this.x = 0;
    }

    public void KeyPress(int keyCode, int gameCode) {
        switch (gameCode) {
            case Canvas.DOWN:
                if (index < menustmp.length - 1) {
                    index++;
                } else {
                    SetMenuTmp();
                }
                break;
            case Canvas.UP:
                if (index > 0) {
                    index--;
                } else {
                    SetMenuTmp();
                }
                break;
        }
        System.out.println(index);
    }

    private void SetMenuTmp() {
        if (menus.length > length && index == length - 1 && end < menus.length) {
            start++;
            end++;
            System.arraycopy(menus, start, menustmp, 0, length);
            return;
        }
        if (index == 0 && start > 0) {
            start--;
            end--;
            System.arraycopy(menus, start, menustmp, 0, length);
            return;
        }
    }

    public void Paint(Graphics g) {
        g.setColor(0xffff33);
        g.fillRoundRect(x, y, width, heightPopup, cornerRadius, cornerRadius);
        g.setColor(0);
        g.drawRoundRect(x, y, width, heightPopup, cornerRadius, cornerRadius);
        int tmpX = x + margin;
        int tmpY = y + margin;
        for (int i = 0; i < menustmp.length; i++) {
            String string = menustmp[i];
            if (index == i) {
                Gradient.DrawGradientBox(g, tmpX, tmpY, width - 2 * margin, font.getHeight());
                g.setColor(0xffffff);
                g.drawString(string, tmpX, tmpY, 0);
            } else {
                g.setColor(colorText);
                g.drawString(string, tmpX, tmpY, 0);
            }
            tmpY += font.getHeight();
        }
    }

    public int GetMenuSelect() {
        return end + index;
    }
}
